let sha1 = require('sha1')
const {token} = require('../uteil')
module.exports = function (req, res, next) {
    console.log('收到验证');

    console.log('data', req.query);
    /*
        {
        signature: '303d58dfc8285d3a45510b9a1db4169456700df2',  //微信的加密签名
        echostr: '5650084502324248542',     //微信的随机字符串
        timestamp: '1590293982',            //微信发送的时间戳
        nonce: '1647499079'                 //微信随机数字
        }
    */
    // 1.将参与签名的三个参数按照字典序组合成数组
    // 2.进行sha1加密
    const { signature, echostr, timestamp, nonce } = req.query;
    let sha1Str = sha1([timestamp, nonce, token].sort().join(''))
    console.log('sha1Str:',sha1Str);
    
    //验证是否和微信的signature一致
    if (sha1Str === signature) {
        res.send(echostr)
    } else {
        res.end('error')
    }

}

// router.get('/', function (req, res, next) {
//   console.log('收到验证');
  
//   console.log('data', req.query);
//   /*
//       {
//       signature: '303d58dfc8285d3a45510b9a1db4169456700df2',  //微信的加密签名
//       echostr: '5650084502324248542',     //微信的随机字符串
//       timestamp: '1590293982',            //微信发送的时间戳
//       nonce: '1647499079'                 //微信随机数字
//       }
//   */
//   // 1.将参与签名的三个参数按照字典序组合成数组
//   // 2.进行sha1加密
//   const { signature, echostr, timestamp, nonce } = req.query;
//   sha1Str = sha1([timestamp, nonce, token].sort().join(''))

//   //验证是否和微信的signature一致
//   if (sha1Str === signature) {
//     res.send(echostr)
//   } else {
//     res.end('error')
//   }

// });